const config = require("./config.js")

const baseUrl = config.baseUrl

const card_check = "dicine/card/check" // 礼品卡信息校验接口(卡绑定)
const scope_guess_address = "scope/guess-address" // 用户填写详细地址时，获取提示输入的下拉列表。
const scope_area_list = "scope/area-list" // 省市区列表获取接口
const order_add_address = "dicine/order/add-address" // 收货地址信息保存接口
const product_child_list = "dicine/product/child-list" // 套餐及虚拟卡商品-子商品列表信息接口
const film_detail = "dicine/film/detail" // 电影详情
const order_add = "dicine/order/add" // 礼品卡确认兑换，选定商品后，提交订单
const cinema_list = "dicine/cinema/list" // 影院列表接口
const wx_set_user_info = "wx/set-user-info" // 用户详情更新接口
const wx_app_login = "wx/app-login" // 通用的小程序用户登录接口
const wx_app_form_id = "wx/app-form-id" // 小程序表单ID信息提交接口
const card_bind = "dicine/card/bind" // 绑定礼品卡，选定商品后提交绑定

const bind_mobile = "dicine/member/bind-mobile" // Bind phone number
const sms_send_code = "sms/send-code" // Get the SMS verification code短信验证
const vms_send_code = "vms/send-code" // Get the VMS verification code语音验证
const cards_list = "dicine/card/list" // Get a gift card list
const card_detail = "dicine/product/detail" // Get the gift card detail
const order_list = "dicine/order/list" // Get a list of orders
const order_detail = "dicine/order/detail" // Get detail of the order
const order_receive = "dicine/order/receive" // Already confim the goods

// 电影相关
const order_film_show_list = "dicine/order/film-show-list" // Film order lists
const order_film_show_detail = "dicine/order/film-show-detail" // Film order detail
const cinema_show_list = "dicine/cinema/show-list" // choose movie from cinema
const film_cinema_list = "dicine/film/cinema-list" // choose cinema by film
const cinema_show_seat_list = "dicine/cinema/show-seat-list" // get cinema seats list
const film_card_list = "dicine/film/card-list" // get useful ticket
const film_list = "dicine/film/list" // get film list in home page
const cinema_city_list = "dicine/cinema/city-list" // get city list
const cinema_lock_seat = "dicine/cinema/lock-seat" // bay movie ticket and lock the seat when selected
const order_add_film_show = "dicine/order/add-film-show" // confirm order of film
const order_pay_film_show = "dicine/order/pay-film-show" // push pay result

// 电子码
const order_add_code = "dicine/order/add-code" // push order detail to server and get electronic code
const order_code_detail = "dicine/order/code-detail" // get electronic code order detail

const mobile_bind_mobile = "zxehr/pass-card/bind-mobile" // 手机号绑定
const mobile_usability_check = "zxehr/e-card/check-user-mobile" // 手机号有效性判断
const mobile_bind_card_check = "zxehr/e-card/check" // 获取手机号关联的卡列表
const mobile_card_bind = "zxehr/e-card/bind" // 手机号获取列表 ->绑卡
const prise_for_company = "zxehr/e-card/fabulous" // 绑卡成功给公司点赞
const get_greet_detail = "zxehr/e-card/greet-card-detail" // 贺卡详情
const greeting_share = "zxehr/e-card/share" // 贺卡分享

const notice_latest = "zxehr/notice/latest" // 获取首页通知栏内容

const address_list = "mebuy/address/list" //地址列表
const address_add = "mebuy/address/create-address" //添加地址
const address_update = "mebuy/address/update-address" //更新地址
const address_delete = "mebuy/address/delete-address" //移除地址

// ---------> 新增的url
const getUserLogin = "mebuy/user/login" // 验证用户参加内购权限
const goods_order_list = "mebuy/order/list" // 订单列表
const order_confirm = "mebuy/order/order-pay-info" // 确认订单
const product_list = "mebuy/product/list" // 商品
const product_detail = "mebuy/product/product-detail" // 商品详情 
const product_statistics = "mebuy/product/statistics" // 商品购买统计
const default_address = "mebuy/address/default-address" // 用户获取默认地址
const order_view = "mebuy/order/view" // 订单详情
// const img_upload = "image-file/upload"
const mulit_upload = "image-file/mulit-upload" // 多图上传
const service_type = "mebuy/order/service-type" // 获取获取售后服务类型
const refund_desc = "mebuy/order/refund-desc" // 退款原因
const confirm_receipt = "mebuy/order/confirm-receipt" // 确认收货
const order_refund = "mebuy/order/order-refund" // 申请售后
const order_service_revoke = "mebuy/order/order-service-revoke" // 撤销售后
const service_list = "mebuy/order/service-list" // 售后列表
const service_view = "mebuy/order/service-view" // 售后列表详情
const get_ship_info = "mebuy/order/get-ship-info" // 物流类型
const service_refund_ship = "mebuy/order/service-refund-ship" // 提交物流单号
const service_history = "mebuy/order/service-history" // 协商历史
// const refund_desc ="mebuy/order/refund-desc" // 确认收货
const network_error = "网络异常,请检查网络"
const no_network = "无网络"

/**
 * 网络请求并返回格式化数据
 * 成功:result
 * 失败:状态码及信息
 */
function getDataResult(url, data, met) {
  return new Promise(function (resolve, reject) {
    wx.getNetworkType({
      success: function (res) {
        if (res.networkType != "none") {

          getDataFormat(url, data, met, resolve, reject)
        } else {
          reject({
            code: 2000,
            msgContent: no_network
          })
        }
      },
      fail: function () {
        // 获取网络状态失败 , 继续拉取
        getDataFormat(url, data, met, resolve, reject)
      }
    })

  })

}

function getDataFormat(url, data, met, resolve, reject) {
  data.userToken = _getUserInfo().userToken;
  _log("requestData", data)
  wx.request({
    url: baseUrl + url,
    data: data,
    method: met == "POST" ? "POST" : "GET",
    success: res => {
      let statusCode = res.statusCode;
      if (statusCode == 200) {
        let code = res.data.code;
        if (code == 1000) {
          resolve(res.data.result)
        } else if (code >= 3000 && code < 4000) {
          // 强提示
          reject({
            code: 3000,
            msgContent: res.data.msgContent
          })
        } else {
          wx.showToast({
            title: res.data.msgContent,
            icon: 'none'
          })
          // 弱提示
          reject({
            code: 2000,
            msgContent: res.data.msgContent
          })
        }
      } else {
        reject({
          code: 2000,
          msgContent: "请求异常: " + statusCode
        })
      }
    },
    fail: res => {
      reject({
        code: 2000,
        msgContent: network_error
      })
    },
    complete: res => {
      _log(url, res)
    }
  })
}

module.exports = {
  getDataResult: getDataResult,
  card_check: card_check,
  scope_guess_address: scope_guess_address,
  scope_area_list: scope_area_list,
  order_add_address: order_add_address,
  product_child_list: product_child_list,
  film_detail: film_detail,
  order_add: order_add,
  cinema_list: cinema_list,
  wx_set_user_info: wx_set_user_info,
  wx_app_login: wx_app_login,
  wx_app_form_id: wx_app_form_id,
  card_bind: card_bind,
  bind_mobile: bind_mobile,
  sms_send_code: sms_send_code,
  vms_send_code: vms_send_code,
  cards_list: cards_list,
  card_detail: card_detail,
  order_list: order_list,
  order_detail: order_detail,
  order_receive: order_receive,
  order_film_show_list: order_film_show_list,
  order_film_show_detail: order_film_show_detail,
  cinema_show_list: cinema_show_list,
  film_cinema_list: film_cinema_list,
  cinema_show_seat_list: cinema_show_seat_list,
  film_card_list: film_card_list,
  film_list: film_list,
  cinema_city_list: cinema_city_list,
  cinema_lock_seat: cinema_lock_seat,
  order_add_film_show: order_add_film_show,
  order_pay_film_show: order_pay_film_show,
  order_add_code: order_add_code,
  order_code_detail: order_code_detail,
  mobile_bind_mobile: mobile_bind_mobile,
  mobile_usability_check: mobile_usability_check,
  mobile_bind_card_check: mobile_bind_card_check,
  mobile_card_bind: mobile_card_bind,
  prise_for_company: prise_for_company,
  get_greet_detail: get_greet_detail,
  greeting_share: greeting_share,
  notice_latest: notice_latest,
  address_list: address_list,
  address_add: address_add,
  address_update: address_update,
  address_delete: address_delete,
  goods_order_list: goods_order_list,
  product_list: product_list,
  product_detail: product_detail,
  product_statistics: product_statistics,
  default_address: default_address,
  order_view: order_view,
  mulit_upload: mulit_upload,
  getUserLogin: getUserLogin,
  order_confirm: order_confirm,
  service_type: service_type,
  refund_desc: refund_desc,
  confirm_receipt: confirm_receipt,
  order_refund: order_refund,
  order_service_revoke: order_service_revoke,
  service_list: service_list,
  get_ship_info: get_ship_info,
  service_refund_ship: service_refund_ship,
  service_view: service_view,
  service_history: service_history
}

/**
 * 用于从内存或缓存获取用户登录信息
 */
function _getUserInfo() {
  let loginResult = getApp().globalData.loginResult;
  let userId;
  if (loginResult != null && loginResult != undefined) {
    userId = loginResult.userId;
  }

  if (userId != undefined && userId != null && userId != 0) {
    return loginResult;
  } else {
    try {
      loginResult = wx.getStorageSync("loginResult")
      userId = loginResult.userId
      if (userId != undefined && userId != null && userId != 0) {
        return loginResult
      } else {
        return {
          userId: 0,
          mobile: "",
          userToken: ""
        }
      }
    } catch (e) {
      return {
        userId: 0,
        mobile: "",
        userToken: ""
      }
    }
  }
}

/**
 * log工具
 */
function _log(name, value) {
  if (config.showLog) {
    console.log(name, value)
  }
}